<?php


namespace app\studentLayer\controller;

use app\studentLayer\model\TeacherModel;
use think\Controller;
use think\Db;

class Teacher extends Controller
{

    //查询老师
    public function SelectTeacher()
    {
        if ($this->request->isPost()) {
            $Select = TeacherModel::SelectTeacherModel();
            //var_dump($Select);die;
            //$this->assign('Select', $Select);
            $this->ajax('', '', $Select);

        } else {

            if ($this->request->isAjax()) {
                $limit = $this->request->get();


                $Select = TeacherModel::SelectTeacherModel();
                $Select = $Select->page($limit['page'], $limit['limit'])->select();

                $SelectCount = TeacherModel::SelectTeacherModel();
                $count = $SelectCount->count();

                  //var_dump($count);die;
                $this->ajax('', '', $Select, $count);
            }

            return $this->fetch('/TeacherView/SelectTeacher');
        }
    }

    //添加
    public function InsertTeacher()
    {


        if ($this->request->isPost()) {

            $InsertData = $this->request->post();

            if (!isset($InsertData['c_name'])) {
                return $this->error('必须选择一个科目', 'Teacher/InsertTeacher');
                die;
            }
            //var_dump($InsertData);die;
            $Insert = TeacherModel::InsertTeacherModel($InsertData);
//           die;
            if ($Insert) {
                return $this->ajax('0', '添加成功');
                //  return $this->success('添加成功', 'Teacher/SelectTeacher');
            } else {
                return $this->error('添加失败', 'Teacher/InsertTeacher');
            }

        } else {

            return $this->fetch('/TeacherView/InsertTeacher');
        }
    }


    public function UpdateTeacher()
    {
        if ($this->request->isPost()) {

            $UpdateData = $this->request->post();
            if (!isset($UpdateData['c_name'])) {
                //var_dump(1);
                return $this->error('必须选择一个科目', 'Teacher/SelectTeacher');
                die;
            }
            //var_dump($UpdateData);die;
            $Update = TeacherModel::UpdateTeacherModel($UpdateData);
            if ($Update) {
                return $this->ajax('', '修改成功');
                //return $this->success('修改成功', 'Teacher/SelectTeacher');
            } else {
                return $this->ajax('', '修改失败');
//                return $this->error('修改失败', 'Teacher/SelectTeacher');
            }

        } else {

            //获取id
            $SelectID = $this->request->get();
            $SelectGet = Db::table('s_teacher')->get($SelectID);
            $this->assign('SelectGet', $SelectGet);
            //var_dump($SelectGet);die;

            //var_dump($SelectID);die;

            $SelectTeaching = Db::table('s_teaching')->where(['t_id' => $SelectID['id']])->select();
//            //var_dump($SelectTeaching);die;
//            // ps :一定要注意数组的 []，因为乱用的话有可能变成了多维数组虽然数据不变，但是一些系统函数用起来却是很麻烦的，比如这次
            $c_id = array_column($SelectTeaching, 'c_id');
//            //var_dump($c_id);die;
//            $this->assign('c_id', $c_id);

            if ($this->request->isAjax()) {
                $SelectGet['c_id'] = $c_id;
                $this->ajax('', '', $SelectGet);

            }


            return $this->fetch('/TeacherView/UpdateTeacher');
        }
    }

    public function DeleteTeacher()
    {
        if ($this->request->isGet()) {
            $SelectID = $this->request->get();
            $Delete = TeacherModel::DeleteTeacher($SelectID);
            if ($Delete) {

                //$this->ajax('','删除成功',$Delete);
                return $this->success('删除成功', 'Teacher/SelectTeacher');
            } else {
                return $this->error('删除失败', 'Teacher/SelectTeacher');
            }

        }

        return $this->fetch('/TeacherView/SelectTeacher');
    }


    //显示学生视图，给学生打分
    public function SelectAchievement()
    {
        /*
         * select s.*,cls.name as cls_name, c.name as c_name,c.id as c_id, t.id as t_id,r.achievement
            from   s_teacher t
            LEFT JOIN s_report  r on t.id = r.t_id
            LEFT JOIN s_student s on s.id = r.s_id
            LEFT JOIN s_classes cls on cls.id = r.cls_id
            LEFT JOIN s_course c on c.id = r.c_id
        */

        $Id = $this->request->get();
        //var_dump($Id);die;
        $Select_Achievement = Db::table('s_teacher')->alias('t')
            ->field('s.*,cls.name as cls_name, c.name as c_name,c.id as c_id, t.id as t_id,r.achievement')
            ->leftJoin('s_report r', 't.id = r.t_id')
            ->leftJoin('s_student s', 's.id = r.s_id')
            ->leftJoin('s_classes cls', 'cls.id = r.cls_id')
            ->leftJoin('s_course c', 'c.id = r.c_id')
            ->where(['t.id' => $Id['id']])->select();

        $this->assign('Select_Achievement', $Select_Achievement);
        return $this->fetch('/TeacherView/SelectAchievement');
    }


    public function Achievement()
    {
        if ($this->request->isPost()) {

            $Insert = $this->request->post();
            //var_dump($Insert);die;
            $Achievement = Db::table('s_report')
                ->where(['s_id' => $Insert['s_id'], 'c_id' => $Insert['c_id']])
                ->update($Insert);

            if ($Achievement) {
                return $this->success('打分成功', '/studentLayer/Teacher_Base/Teacher_Base');
            } else {
                return $this->success('打分失败', '/studentLayer/Teacher_Base/Teacher_Base');
            }
        } else {

//            select * from s_report where s_id = 81 and c_id = 6

            $Achievement = $this->request->get();

            //$Achievement = Db::table('s_report')->where($Select)->select();
            //var_dump($Achievement);die;
            // var_dump($Select);die;
            $this->assign('Achievement', $Achievement);
            return $this->fetch('/TeacherView/Achievement');
        }
    }


    public function ajax(string $code, string $msg, array $ajaxdata = [], $count = NULL)
    {
        $ajax_Teacher = [
            'code' => $code ? $code : 0,
            'msg' => $msg ? $msg : null,
            'ajaxdata' => $ajaxdata,
            'count' => $count
        ];

        echo(json_encode($ajax_Teacher));
        die;
    }


}